package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.entity.Role;

import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/roles")
@RestController
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    /**
     * 查询分页角色列表
     */
   @GetMapping
    public Result list(
            String name,
            String label,
            @RequestParam (defaultValue = "1") Integer page,
            @RequestParam (defaultValue = "10") Integer pageSize){
      PageResult<Role> list = roleService.roleList(name,label,page,pageSize);
        return Result.success(list);
    }
    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){

        roleService.delete(id);
        return Result.success();
    }
    /**
     * 插入角色
     */
    @PostMapping
    public Result add(@RequestBody Role role){
        roleService.add(role);
        return Result.success();
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    /**
     * 修改角色
     */
    @PutMapping
    public Result update(@RequestBody Role role){
        roleService.update(role);
        return Result.success();
    }
    /**
     * 查询所有的角色
     */
    @GetMapping("/list")
    public Result listAll(){
        List<Role> list = roleService.listAll();
        return Result.success(list);
    }

}
